﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Data;
using ClassLibrary.Entity;

public partial class UserControl_WebUserControl : System.Web.UI.UserControl
{
    TblCustomersDAO CustomersDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblTitlePage.Text = Global.GetPageName(Request.ServerVariables["SCRIPT_NAME"].ToString());
            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                LoadState();
                LoadSuburb();
            }
            else
            {
                txtFName.Text = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerFirstname;
                txtLName.Text = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerLastname;
                txtEmail.Text = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).Emailaddress;
                txtPass.Enabled = false;
                lbpass.Enabled = false;
                txtEmail.Enabled = false;
                LoadState();
                LoadSuburb();
                loadCustomerDetail();
            }
        }      
    }

    private void loadCustomerDetail()
    {
        int id = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
        DataTable dt = CustomersDAO.GetCustomerId(id);
        foreach (DataRow customerdt in dt.Rows)
        {
            txtCompany.Text = customerdt["Companyname"].ToString();
            txtEmail.Text = customerdt["emailaddress"].ToString();
            drTitle.SelectedValue = customerdt["CustomerIndex"].ToString();
            txtFName.Text = customerdt["CustomerFirstname"].ToString();
            txtLName.Text = customerdt["CustomerLastname"].ToString();
            txtMobile.Text = customerdt["phonemobile"].ToString();
            txtNumber.Text = customerdt["postalnumber"].ToString();
           // txtPass.Enabled = false;
            txtPostcode.Text = customerdt["postalpostcode"].ToString();
            txtStreet.Text = customerdt["postalstreet"].ToString();
           // txtUsername.Text = customerdt["CustomerUsername"].ToString();
            txtWorkPhone.Text = customerdt["phonework"].ToString();
            txtFax.Text = customerdt["phonefacsimile"].ToString();
            txtHomePhone.Text = customerdt["phonehome"].ToString();
            LoadState();
            LoadSuburb();
            //drSuburb.SelectedValue = customerdt["postalsuburb"].ToString();
            //drState.SelectedValue = customerdt["postalstate"].ToString();
            drSuburb.Items.Insert(0, new ListItem(customerdt["postalsuburb"].ToString(), " "));
            drSuburb.SelectedIndex = -1;

            drState.Items.Insert(0, new ListItem(customerdt["postalstate"].ToString(), " "));
            drState.SelectedIndex = -1;
        }
    }
    private void LoadState()
    {
        drState.DataSource = CustomersDAO.GetState();
        drState.DataTextField = "state";
        drState.DataValueField = "state";
        drState.DataBind();
    }
    private void LoadSuburb()
    {
        drSuburb.DataSource = CustomersDAO.GetSuburb(drState.SelectedValue, txtPostcode.Text);
        drSuburb.DataTextField = "suburb";
        drSuburb.DataValueField = "state";
        drSuburb.DataBind();
    }
    protected void txtPostcode_TextChanged(object sender, EventArgs e)
    {
        LoadSuburb();
    }
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblCustomers customer = new TblCustomers();

            customer.CustomerTitle = drTitle.SelectedValue;
            customer.CustomerFirstname = txtFName.Text;
            customer.CustomerLastname = txtLName.Text;
            customer.CustomerFullname = drTitle.SelectedValue.ToString() + " " + txtFName.Text + " " + txtLName.Text;
            customer.Companyname = txtCompany.Text;
            customer.Postalnumber = txtNumber.Text;
            customer.Postalstreet = txtStreet.Text;
            customer.Postalsuburb = drSuburb.SelectedItem.Text;
            customer.Postalpostcode = txtPostcode.Text;
            customer.Postalstate = drState.SelectedValue;
            customer.Phonework = txtWorkPhone.Text;
            customer.Phonehome = txtHomePhone.Text;
            customer.Phonemobile = txtMobile.Text;
            customer.Phonefacsimile = txtFax.Text;
            customer.Emailaddress = txtEmail.Text;
            //customer.CustomerUsername = txtUsername.Text;
            customer.Emailpassword = Encryption.EncryptToString(txtPass.Text);
            customer.PreferContactMethod =Convert.ToInt32(drPreConMeth.SelectedValue);
            customer.DateCreated = DateTime.Now;

            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                customer.CustomerIndex = 0;
                CustomersDAO.InsUpdCustomers(customer, 1);
            }
            else
            {
                customer.CustomerIndex = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
                CustomersDAO.InsUpdCustomers(customer, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
            lblResult.Text = "successful";
    }
    protected void btnExit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnNewBookIn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Booking.aspx");
    }
}
